package org.springframework.beanLifeCycle.methodTemplatePattern;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: T-jack
 * @Date: 2024-01-03-14:47
 * @Description: 此处为模板方法的示例代码
 * 作用 添加代码的扩展性
 * 适用场景 存在部门代码需要动态配置
 */
public class TestMethodTemplatePattern {
	public static void main(String[] args) {
		MyBeanFactory beanFactory = new MyBeanFactory();
		beanFactory.addBeanPostProcessor(bean -> System.out.println("解析 @Autowired"));
		beanFactory.addBeanPostProcessor(bean -> System.out.println("解析 @Resource"));
		beanFactory.getBean();
	}

	static class MyBeanFactory {
		public Object getBean() {
			Object bean = new Object();
			System.out.println("构造：" + bean);
			System.out.println("依赖注入：" + bean);
			for (BeanPostProcessor processor : processors) {
				processor.inject(bean);
			}
			System.out.println("初始化：" + bean);
			return bean;
		}

		private List<BeanPostProcessor> processors = new ArrayList<>();

		public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
			processors.add(beanPostProcessor);
		}
	}

	interface BeanPostProcessor {
		void inject(Object bean);
	}
}
